/**
 * 
 */
package games;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.PrintStream;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 * @author Owner
 *
 */
public abstract class Game extends JPanel {
	protected static final Font HEADER_FONT=new Font(Font.SERIF,Font.BOLD,20);
	protected final FontMetrics hfm=getFontMetrics(HEADER_FONT);
	
	protected static final String SEPARATOR="\t";
	protected static final String PLAYER_NAME="Player";
	protected static final String SUFFIX=".data";
	protected static final String DATA_FOLDER="games\\data\\";
	
	Runner runner;
	PrintStream log;
	String id;
	Timing clock;
	int time;
	boolean gameCompleted;
	Painter go;
	Painter stop;
	JPanel header,main,footer;
	
	String name;

	/**
	 * 
	 */
	public Game(Runner runner,String gameName) {
		this.runner=runner;
		log=runner.log;
		id=runner.id;
		log("starting "+gameName);
		gameCompleted=false;
	}
	
	public abstract void init();
	public abstract void start();
	
	public void log(String action,int time) {
		log.println(action+"\t"+time);
		log.flush();
	}
	
	public void log(String action,double money) {
		log.println(action+"\t"+String.format("$%.2f",money));
		log.flush();
	}
	
	public void log(String action) {
		log.println(action);
		log.flush();
	}
	
	public void hmmm(String n) {
		clock=new Timing();
		proceed();
		name=n;
		
    addMouseListener(new MouseAdapter(){
      public void mousePressed(MouseEvent e){
        //get the mouse position
        //int xPos=e.getX();
        //int yPos=e.getY();
      	suspend();
      	time=clock.getElapsedTime();
        finished(name,time);
      }
    });
	}
	
  public synchronized void proceed() {
    if (false) {
      System.err.println("cannot restart completed game");
      return;
    }
    //gameValue = "proceeding";
    if (clock.isSuspended()) {
      clock.proceed();
      //sessionLog.println("game proceeding " + System.currentTimeMillis());
    } else
      System.err.println("game still proceeding");
    //manager.bulletin(getTitle() + " in progress");
  }
  
  public synchronized void suspend() {
    //gameValue = "suspended";
    if (!clock.isSuspended()) {
      clock.suspend();
      //sessionLog.println("game suspended " + System.currentTimeMillis());
    }
    //System.err.println("game suspended");
    //manager.bulletin(getTitle() + " suspended");
  }
  
  public synchronized void finished(String game, int time) {
    suspend();
  	gameCompleted = true;
    log("finished "+game,time);
    runner.finished(game,time);
    //System.err.println("message from game Supervisor: " + message);
    //sessionLog.println(message + "  " + System.currentTimeMillis());
    //Toolkit.getDefaultToolkit().beep();
    //gameValue = message;
    //showPayoffs(time);
    // gameLog.println("finished "+getSource());
  }
  
  public BufferedImage loadImage(String name) {
    String imgFileName = "images/"+name+(name.endsWith("2")?".bmp":".png");
    URL url = Game.class.getResource(imgFileName);
    BufferedImage img = null;
    try {
        img =  ImageIO.read(url);
    } catch (Exception e) {
    	System.err.println("Problem reading image "+name);
    	e.printStackTrace();
    }
    return img;
	}
  
  class Painter extends Component {
    
    /**
		 * 
		 */
		private static final long serialVersionUID=1L;
		
		BufferedImage img = null;
    String label;
		int width;
    int cWidth;
    int height;
    
    public Painter(String imgStr,String label) {
    	img=loadImage(imgStr);
    	this.label=label;
    	width=img.getWidth();
    	height=hfm.getHeight()+10;
    }
    
    public Dimension getPreferredSize(){
      return new Dimension(width+100,height+img.getHeight()+10);
  	}
    
    public void paint(Graphics g) {
      g.setFont(HEADER_FONT);
      cWidth=getWidth();
      g.drawString(label,(cWidth-width)/2,height);
      g.drawImage(img,(cWidth-width)/2,height,null);
  	}
  }
}
